<template>
    <div class="Regdiv1">
        <h1 class="Regdivh1">hggood</h1>
        <h2 class="divh2">品牌特卖</h2>
        <el-form status-icon ref="form" :model="form" :rules="rules" label-width="80px" class="register-box">
        <h3 class="login-title">欢迎注册</h3>
            <el-form-item label="账号" prop="username" style="margin-left:50px">
                <el-input type="text" placeholder="请输入用户名" v-model="form.username"
                style="width:200px;margin-left:-50px"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" style="margin-left:50px">
                <el-input type="password" placeholder="请输入密码" v-model="form.password"
                style="width:200px;margin-left:-50px"></el-input>
            </el-form-item>
            <el-button type="primary" @click="submitForm('form')">立即创建</el-button>
            <el-button @click="resetForm('form')">重置</el-button>
            <el-button  class="Reggologin" @click="Reggologin()">我要登陆</el-button>
        </el-form>
    </div>
</template>

<script>
import axios from 'axios'
import Vue from 'vue'
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)
    export default {
        name:'PxReg',
        data() {
            return {
                form:{
                    username:'',
                    password:'',
                },
                rules:{
                    username:[{
                        required: true,
                        message:'请输入用户名',
                        trigger:'blur'
                    }],
                    password:[{
                        required: true,
                        message:'请输入密码',
                        trigger:'blur'
                    }],
                }
            }
        },
        mounted(){
            // this.$cookies.set("token","dasdasdsadsadasad","60s")
            // this.$cookies.set("token22","dttttttttttsad","60s")
            // let test = this.$cookies.get("csrftoken")
            // console.log(test,'%%%%');
            // window.$VueCookies.get("")
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                if (valid) {
                    // {"${_csrf.parameterName}":"${_csrf.token}"}
                    var data = new URLSearchParams();
                    data.append('username',this.form.username)
                    data.append('password',this.form.password)
                    console.log('&&&',this.form.username,'@@@',this.form.password);
                    this.$addr.get(`users/reg`,{
                    params:{
                        username:this.form.username,
                        password:this.form.password
                    }
                    // this.$addr.post(`users/reg?username=${this.form.username}&password=${this.form.password}`
                    }).then(
                        response=>{
                            console.log('请求成功了',response.data)
                            if( response.data.state == 4000){
                                this.$message({
                                    message: '用户名被占用了',
                                    type: 'error'
                                });  
                            }else{
                                this.$message({
                                    message: '注册成功',
                                    type: 'success'
                                });
                                this.$router.push({name:'PxLogin',params:{'username':this.form.username}})
                            }

                        },
                        error=>{
                            this.$message({
                                message: '呕吼请求失败了',
                                type: 'warning'
                            });
                            console.log('请求失败了',error.message)
                        }
                    )
                } else {
                    this.$message({
                        message: '用户名或密码错误',
                        type: 'warning'
                    });
                    return false;
                }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            Reggologin(){
                this.$router.push({name:'PxLogin'})
            }
        },
    }
</script>

<style scoped>
.Regdiv1{
    text-align: center;
    margin: 0 auto;
}
.Regdivh1{
    margin-top: 120px;
    color: rgb(255, 36, 120);
}
    .Reggologin{
        background-color: rgb(105, 211, 214);
    }
</style>